package org.mrjbee.share.it.easy.client.daemon;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class ConfigurationProvider {

	private Properties properties;
	private String fileName;
	
	public void initFromFile(String string) throws FileNotFoundException, IOException {
		Log.SYS.info("Start consuming properties from file:"+string);
		properties = new Properties();
		properties.load(new FileReader(string));
		fileName = string;
	}

	public void init(Properties properties) {
		Log.SYS.debug("Init with inline properties");
		this.properties = properties;
	}

	public String getServerBaseUrl() {
		return properties.getProperty("server.base.url", "http://localhost:6677/shared.clipboard.server");
	}

	public String notificationPattern() {
		return properties.getProperty("notifications.pattern");
	}

	public boolean isTokenExists() {
		return (getAccessToken()!=null 
				&& getTokenSecret()!=null);
	}

	public String getTokenSecret() {
		return properties.getProperty("access.token.secret");
	}

	public String getAccessToken() {
		return properties.getProperty("access.token");
	}

	public boolean isGAEManagerRequired() {
		return getServerBaseUrl().trim().equals("https://shareiteasyserver.appspot.com");
	}

	public void storeTokenInfo(String[] tokenInfo) {
		properties.setProperty("access.token", tokenInfo[0]);
		properties.setProperty("access.token.secret", tokenInfo[1]);
		if (fileName != null){
			try {
				properties.store(new FileWriter(fileName), "Updated with tokens");
			} catch (IOException e) {
				Log.SYS.error("Error during saving tokens", e);
			}
		}
	}

}
